package org.example.springextension.listener;


import com.rabbitmq.client.Channel;
import org.example.springextension.constants.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;



@Component
public class QosListener {
    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void handleMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            System.out.printf("111接收到消息: %s, deliveryTag: %d \n", new String(message.getBody(),"UTF-8"),
                    message.getMessageProperties().getDeliveryTag());
            Thread.sleep(2000);
            channel.basicAck(deliveryTag, false);
        }catch (Exception e) {
            channel.basicNack(deliveryTag,false,true);
        }
    }
    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void handleMessage2(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            System.out.printf("222接收到消息: %s, deliveryTag: %d \n", new String(message.getBody(),"UTF-8"),
                    message.getMessageProperties().getDeliveryTag());
            Thread.sleep(1000);
            channel.basicAck(deliveryTag, false);
        }catch (Exception e) {
            channel.basicNack(deliveryTag,false,true);
        }
    }
}
